<script lang="ts" setup>
import { useUserStore } from '@/stores/user'
import { ElMessage, type UploadFile } from 'element-plus'
import { ref } from 'vue'
import { downloadAvatarUtil, uploadAvatarUtil } from '@/AGC/utils/user'

const store = useUserStore()
const imageUrl = ref('')

// 上传头像
const uploadFile = (file: UploadFile) => {
  imageUrl.value = URL.createObjectURL(file.raw!)
  uploadAvatarUtil(file)
}
// 下载头像
const downLoadFile = async () => {
  try {
    imageUrl.value = await downloadAvatarUtil()
  } catch (error) {
    ElMessage.error('获取头像失败')
    Promise.reject(error)
  }
}
downLoadFile()
</script>

<template>
  <el-card>
    <template #header>
      <div class="card-header">个人信息</div>
    </template>
    <div class="avatar-section">
      <el-upload
        action
        class="avatar-uploader"
        :show-file-list="false"
        :on-change="uploadFile"
        :auto-upload="false"
      >
        <img v-if="imageUrl" :src="imageUrl" class="avatar" />
        <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
      </el-upload>
    </div>
    <div class="info-item">
      <div>
        <el-icon><User /></el-icon> 昵称
      </div>
      <div>{{ store.userInfo.nickname }}</div>
    </div>
    <div class="info-item">
      <div>
        <el-icon><User /></el-icon> 手机号码
      </div>
      <div>{{ store.userInfo.phone }}</div>
    </div>
    <div class="info-item">
      <div>
        <el-icon><User /></el-icon> 邮箱
      </div>
      <div>{{ store.userInfo.email }}</div>
    </div>
    <div class="info-item">
      <div>
        <el-icon><User /></el-icon> 性别
      </div>
      <div>{{ store.userInfo.sex === 1 ? '男' : store.userInfo.sex === 0 ? '女' : '未知' }}</div>
    </div>
    <div class="info-item">
      <div>
        <el-icon><User /></el-icon> 年龄
      </div>
      <div>{{ store.userInfo.age }}</div>
    </div>
    <div class="info-item">
      <div>
        <el-icon><User /></el-icon> 身高
      </div>
      <div>{{ store.userInfo.height }}</div>
    </div>
    <div class="info-item">
      <div>
        <el-icon><User /></el-icon> 体重
      </div>
      <div>{{ store.userInfo.weight }}</div>
    </div>
  </el-card>
</template>

<style lang="scss" scoped>
.el-card {
  width: 25%;

  .avatar-section {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 150px;

    .avatar-uploader .avatar {
      width: 120px;
      height: 120px;
      border-radius: 50%;
      display: block;
    }

    .avatar-uploader .el-upload {
      border: 1px dashed var(--el-border-color);
      border-radius: 50%;
      cursor: pointer;
      position: relative;
      overflow: hidden;
      transition: var(--el-transition-duration-fast);
    }

    .avatar-uploader .el-upload:hover {
      border-color: var(--el-color-primary);
    }

    .el-icon.avatar-uploader-icon {
      font-size: 28px;
      color: #8c939d;
      width: 120px;
      height: 120px;
      border-radius: 50%;
      text-align: center;
    }
  }

  .info-item {
    display: flex;
    justify-content: space-between;
    align-items: center;
    border-bottom: 1px solid rgb(220, 223, 230);
    padding: 13px 0;
  }
}
</style>
